﻿using BomComparisonModule.Views.ResultDialog;

namespace BomComparisonModule.ViewModels.ResultDialog
{





    public class ShowResultViewModel : BindableBase, IDialogAware
    {
        #region 定义变量
        private string _title = "Prism Unity Application";
        private string _message;
        private IRegionManager _regionManager;

        private readonly IContainerExtension _container;

        #endregion

        public IRegionManager RegionManager
        {
            get => _regionManager;
            set
            {
                _regionManager = value;
                //OnPropertyChanged();
            }
        }

        private DelegateCommand<string> _closeDialogCommand;
        public DelegateCommand<string> NavigateCommand { get; private set; }
        public DelegateCommand<string> CloseDialogCommand => _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog));

        //public event Action<IDialogResult> RequestClose;

        public DialogCloseListener RequestClose { get; }

        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public IRegionManager ShowResultDialogRegionManager
        {
            get => _regionManager;
            set
            {
                _regionManager = value;
                //OnPropertyChanged();
            }
        }



        public ShowResultViewModel(IRegionManager regionManager, IContainerExtension container)
        {
            //创建RegionManager
            RegionManager = regionManager.CreateRegionManager();
            //_regionManager = regionManager.CreateRegionManager();
            _container = container;

            //显示导航视图
            _regionManager.RegisterViewWithRegion("ShowResultDialog.ModuleContentRegion", typeof(BOM1View));
            _regionManager.RegisterViewWithRegion("ShowResultDialog.ModuleContentRegion", typeof(BOM2View));
            _regionManager.RegisterViewWithRegion("ShowResultDialog.ModuleContentRegion", typeof(BOMDiffView));

            NavigateCommand = new DelegateCommand<string>(Navigate2);
        }

        private void Navigate2(string navigatePath)
        {
            if (navigatePath != null)
                _regionManager.RequestNavigate("ShowResultDialog.ModuleContentRegion", navigatePath);
        }

        protected virtual void CloseDialog(string parameter)
        {
            ButtonResult result = ButtonResult.None;

            if (parameter?.ToLower() == "true")
                result = ButtonResult.OK;
            else if (parameter?.ToLower() == "false")
                result = ButtonResult.Cancel;

            RaiseRequestClose(new DialogResult(result));
        }


        public virtual void RaiseRequestClose(IDialogResult dialogResult)
        {
            // Option 1.
            RequestClose.Invoke();

            // Option 2.
            RequestClose.Invoke(new DialogParameters { { "MyParameter", dialogResult } });

            // Option 3.
            RequestClose.Invoke(ButtonResult.OK);

            // Option 4.
            RequestClose.Invoke(new DialogParameters { { "MyParameter", dialogResult } }, ButtonResult.OK);

            // Option 5.
            var result = new DialogResult
            {
                Parameters = new DialogParameters { { "MyParameter", dialogResult } },
                Result = ButtonResult.OK
            };
            RequestClose.Invoke(result);
        }

        public virtual bool CanCloseDialog()
        {
            return true;
        }

        public virtual void OnDialogClosed()
        {
        }

        public virtual void OnDialogOpened(IDialogParameters parameters)
        {
            Message = parameters.GetValue<string>("message");
        }

        //DialogCloseListener IDialogAware.RequestClose => throw new NotImplementedException();


    }

}
